<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <el-row :gutter="20">
                        <el-col :span="12" v-if="false">
                            <FormItem
                                :label="t('houses.name')"
                                type="string"
                                v-model="baTable.form.items!.name"
                                prop="name"
                                :placeholder="t('Please input field', { field: t('houses.name') })"
                        /></el-col>
                    </el-row>
                    <el-row :gutter="20">
                        <el-col :span="12">
                            <FormItem
                                :label="t('houses.category_ids')"
                                type="remoteSelects"
                                v-model="baTable.form.items!.category_ids"
                                prop="category_ids"
                                :input-attr="{ pk: 'ba_category.id', field: 'name', 'remote-url': '/admin/category/index' }"
                                :placeholder="t('Please select field', { field: t('houses.category_ids') })"
                        /></el-col>
                    </el-row>
                    <el-row :gutter="20">
                        <el-col :span="12">
                            <FormItem
                                :label="t('houses.industry') + '上级'"
                                v-model="baTable.form.items!.industry_pid"
                                type="remoteSelect"
                                :input-attr="{
                                    pk: 'ba_tags.id',
                                    field: 'string',
                                    params: { type: 'hangye' },
                                    'remote-url': '/admin/tags/index?type=hangye',
                                    onRow: onRemoteSelectRow,
                                }"
                                prop="industry_pid"
                                :placeholder="t('Please select field', { field: t('houses.industry') })"
                            />
                            <FormItem
                                :label="t('houses.industry')"
                                v-model="baTable.form.items!.industry"
                                type="remoteSelects"
                                :input-attr="{
                                    pk: 'ba_tags.id',
                                    field: 'string',
                                    'remote-url': '/admin/tags/index?type=hangye&action=select&pid=' + baTable.form.items!.industry_pid,
                                }"
                                prop="industry"
                                :placeholder="t('Please select field', { field: t('houses.industry') })"
                            />
                        </el-col>
                    </el-row>

                    <el-row :gutter="20">
                        <el-col :span="12">
                            <el-form-item :label="t('联系电话')">
                                <el-input
                                    type="string"
                                    prop="contact_phone"
                                    v-model="baTable.form.items!.contact_phone"
                                    :placeholder="t('Please input field', { field: t('联系电话') })"
                                /></el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="20" v-if="false">
                        <el-col :span="12">
                            <FormItem
                                :label="t('houses.surround')"
                                v-model="baTable.form.items!.surround"
                                type="remoteSelects"
                                :input-attr="{ pk: 'ba_tags.id', field: 'string', 'remote-url': '/admin/tags/index?type=zhoubian' }"
                                prop="surround"
                                :placeholder="t('Please select field', { field: t('houses.surround') })"
                        /></el-col>
                        <el-col :span="12">
                            <FormItem
                                :label="t('houses.property')"
                                type="remoteSelects"
                                :input-attr="{ pk: 'ba_tags.id', field: 'string', 'remote-url': 'admin/tags/index' }"
                                v-model="baTable.form.items!.property"
                                prop="property"
                                :placeholder="t('Please select field', { field: t('houses.property') })"
                        /></el-col>
                    </el-row>
                    <el-row :gutter="20">
                        <el-col :span="12">
                            <el-form-item :label="t('houses.acreage')">
                                <el-input
                                    type="number"
                                    prop="acreage"
                                    :input-attr="{ step: 1 }"
                                    v-model.number="baTable.form.items!.acreage"
                                    :placeholder="t('Please input field', { field: t('houses.acreage') })"
                                    ><template #append>㎡</template></el-input
                                ></el-form-item
                            >
                        </el-col>

                        <el-col :span="12">
                            <el-form-item :label="t('houses.rent_money')">
                                <el-input
                                    type="number"
                                    prop="rent_money"
                                    :input-attr="{ step: 1 }"
                                    v-model.number="baTable.form.items!.rent_money"
                                    :placeholder="t('Please input field', { field: t('houses.rent_money') })"
                                >
                                    <template #append>元/月</template>
                                </el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="20">
                        <el-col :span="12">
                            <FormItem
                                :label="t('houses.city')"
                                type="city"
                                v-model="baTable.form.items!.city"
                                prop="city"
                                :placeholder="t('Please select field', { field: t('houses.city') })"
                            />
                        </el-col>
                        <el-col :span="12">
                            <FormItem
                                :label="t('houses.address')"
                                type="string"
                                v-model="baTable.form.items!.address"
                                prop="address"
                                :placeholder="t('Please input field', { field: t('houses.address') })"
                            />
                        </el-col>
                    </el-row>
                    <span style="margin-left: 120px;color: red;">图片上传请小于400kb</span>
                    <span>
                        <a href="https://tinify.cn/" target="_blank">图片压缩地址</a>
                    </span>
                    <FormItem :label="t('houses.image')" type="image" v-model="baTable.form.items!.image" prop="image" />
                    <FormItem :label="t('houses.images')" type="images" v-model="baTable.form.items!.images" prop="images" />

                    <el-row :gutter="20"><el-col :span="24">
                        <FormItem
                            :label="t('houses.introduction')"
                            type="textarea"
                            v-model="baTable.form.items!.introduction"
                            prop="introduction"
                            :input-attr="{ rows: 3 }"
                            @keyup.enter.stop=""
                            @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                            :placeholder="t('Please input field', { field: t('houses.introduction') })"
                    /></el-col>
                    </el-row>
                    <el-row :gutter="20">
                        <el-col :span="12">
                            <FormItem
                                :label="t('houses.has_bounty')"
                                type="radio"
                                border
                                v-model="baTable.form.items!.has_bounty"
                                prop="has_bounty"
                                :data="{ content: { 0: '否', 1: '是' }, childrenAttr: { border: true } }"
                                :placeholder="t('Please select field', { field: t('houses.has_bounty') })"
                        /></el-col>
                        <el-col :span="12">
                            <FormItem
                                v-if="parseInt(baTable.form.items!.has_bounty) === 1"
                                :label="t('houses.reward')"
                                type="string"
                                prop="reward"
                                v-model="baTable.form.items!.reward"
                                :placeholder="t('Please input field', { field: t('houses.reward') })"
                        /></el-col>
                    </el-row>
                    <el-row :gutter="20" v-if="false">
                        <el-col :span="24">
                            <FormItem
                                :label="t('houses.paymethod')"
                                type="radio"
                                v-model="baTable.form.items!.paymethod"
                                prop="paymethod"
                                :data="{
                                    content: {
                                        '1': t('houses.paymethod 1'),
                                        '3': t('houses.paymethod 3'),
                                        '10': t('houses.paymethod 10'),
                                        '6': t('houses.paymethod 6'),
                                        '12': t('houses.paymethod 12'),
                                    },
                                    childrenAttr: { border: true },
                                }"
                                :placeholder="t('Please select field', { field: t('houses.paymethod') })"
                        /></el-col>
                    </el-row>
                    <el-row :gutter="20" v-if="false">
                        <el-col :span="12">
                            <el-form-item :label="t('houses.start_rent')">
                                <el-input
                                    type="string"
                                    v-model="baTable.form.items!.start_rent"
                                    prop="start_rent"
                                    :placeholder="t('Please input field', { field: t('houses.start_rent') })"
                                >
                                    <template #append>个月</template>
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item :label="t('houses.rent_free')">
                                <el-input
                                    type="string"
                                    v-model="baTable.form.items!.rent_free"
                                    prop="rent_free"
                                    :placeholder="t('Please input field', { field: t('houses.rent_free') })"
                                >
                                    <template #append>个月</template>
                                </el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <FormItem
                        :label="t('houses.user_ids')"
                        type="remoteSelect"
                        v-model="baTable.form.items!.user_id"
                        prop="user_id"
                        :input-attr="{ pk: 'ba_user.id', field: 'mobile', 'remote-url': 'admin/user.user/index' }"
                        :placeholder="t('Please select field', { field: t('houses.user_ids') })"
                    />
                    <FormItem
                        :label="t('排序')"
                        type="number"
                        prop="weigh"
                        :input-attr="{ step: 1 }"
                        v-model.number="baTable.form.items!.weigh"
                        :placeholder="t('Please input field', { field: t('houses.weigh') })"
                    />
                    <FormItem v-if="false"
                        :label="t('houses.prorease_period')"
                        type="date"
                        prop="prorease_period"
                        v-model="baTable.form.items!.prorease_period"
                        :placeholder="t('Please input field', { field: t('houses.prorease_period') })"
                    />
                    <FormItem v-if="false"
                        :label="t('租金单位')"
                        type="radio"
                        border
                        v-model="baTable.form.items!.rent_unit"
                        prop="rent_unit"
                        :data="{ content: { '0': t('元/月'), '1': t('元/㎡/月'), 2: t('元/年') }, childrenAttr: { border: true } }"
                        :placeholder="t('Please select field', { field: t('租金单位') })"
                    />
                    <FormItem  v-if="false" :label="t('houses.files')" type="files" v-model="baTable.form.items!.files" prop="files" />
                    <FormItem
                        :label="t('houses.status')"
                        type="radio"
                        border
                        v-model="baTable.form.items!.status"
                        prop="status"
                        :data="{ content: { '0': t('下架'), '1': t('上架') } }"
                        :placeholder="t('Please input field', { field: t('houses.status') })"
                    />
                    <FormItem
                        :label="t('houses.is_vip')"
                        type="radio"
                        border
                        v-model="baTable.form.items!.is_vip"
                        prop="status"
                        :data="{ content: { '0': t('否'), '1': t('是') } }"
                        :placeholder="t('Please input field', { field: t('houses.status') })"
                    />
                    <el-form-item :label="t('houses.view_price')">
                        <el-input
                            type="string"
                            v-model="baTable.form.items!.view_price"
                            prop="view_price"
                            :placeholder="t('Please input field', { field: t('houses.view_price') })"
                        >
                            <template #append>元</template>
                        </el-input>
                    </el-form-item>
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { onMounted, inject, reactive, ref, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'
import { customFieldsSave, getCustomFields } from '/@/api/backend/hourse'
const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()
const hangye_pid = ref(0)
const onRemoteSelectRow = (rowData: any) => {
    console.log(rowData, '>>>>>')
    hangye_pid.value = rowData.id
    baTable.form.items!.industry = []
}
const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    status: [buildValidatorData({ name: 'number', title: t('houses.status') })],
})
const cusfieldArray = ref<any[]>([])
const initCustomFields = () => {
    cusfieldArray.value = []
    getCustomFields()
        .then((result) => {
            let checked_fields = result.data.checked_fields
            if (checked_fields.length > 0) {
                checked_fields.forEach((item: any) => {
                    rules[item] = [buildValidatorData({ name: 'required', title: t('houses.' + item) })]
                })
            }
        })
        .catch((err) => {
            console.log(err)
        })
}
watch(
    () => baTable.form.operate,
    (val) => {
        if (['Add', 'Edit'].includes(baTable.form.operate!)) {
            initCustomFields()
        }
    }
)
</script>

<style scoped lang="scss"></style>
